%{                                                            /* -*- C++ -*- */

#include "parser.hh"
#include "FServer/MySQL/MySQLScanner.h"
#include "FServer/MySQL/MySQLParser.h"

/*  Defines some macros to update locations */


#define STEP()                                      \
  do {                                              \
    parser.location_->step ();                      \
  } while (0)

#define COL(Col)				                    \
  parser.location_->columns (Col)

#define LINE(Line)				                    \
  do{						                        \
    parser.location_->lines (Line);		            \
 } while (0)

#define YY_USER_ACTION				                \
  COL(yyleng);


typedef keeper_server::Parser::token token;
typedef keeper_server::Parser::token_type token_type;

#define yyterminate() return token::TOK_EOF

#define RETURN_TOKEN(a_token) return keeper_server::Parser::token::a_token;

%}

%option debug
%option c++
%option noyywrap
/*
%option never-interactive
%option yylineno
*/
%option nounput
%option batch
%option prefix="parse"
%option case-insensitive

/*
%option stack
*/

/* Abbreviations.  */

blank   [ \t]+
eol     [\n\r]+

%%

 /* The rules.  */
%{
  STEP();
%}

SELECT        {RETURN_TOKEN(SELECT)}
LIMIT         {RETURN_TOKEN(LIMIT)}

[0-9]*[A-Za-z\200-\377$_][A-Za-z\200-\3770-9$_]*  {
                yylval->strval = stmt.create_char_string(yytext);
                return token::NAME;
              }

[0-9]+	      {
                yylval->strval = stmt.create_char_string(yytext);
                return token::INTNUM;
              }

  /* session variables*/
@@[0-9a-z_$]+ {
                yylval->strval = stmt.create_char_string(yytext+2);
                return token::SESSIONVAR;
              }
{blank}       STEP();

{eol}         LINE(yyleng);

.             {
                std::cerr << *parser.location_ << " Unexpected token : "
                                              << *yytext << std::endl;
                parser.error_ = (parser.error_ == 127 ? 127
                                : parser.error_ + 1);
                STEP ();
              }

%%


/*

   CUSTOM C++ CODE

*/

namespace keeper_server
{

    Scanner::Scanner()
    : parseFlexLexer()
    {
    }

    Scanner::~Scanner()
    {
    }

    void Scanner::set_debug(bool b)
    {
        yy_flex_debug = b;
    }
}

#ifdef yylex
# undef yylex
#endif

int parseFlexLexer::yylex()
{
  std::cerr << "call parsepitFlexLexer::yylex()!" << std::endl;
  return 0;
}
